const peer = require("./peer-control");

// 监听 add-stream 播放视频流
peer.on("add-stream", (stream) => {
  play(stream);
});

// 播放视频流
let video = document.getElementById("screen-video");
function play(stream) {
  video.srcObject = stream;
  video.onloadedmetadata = function () {
    video.play();
  };
}

// 监听键盘事件
window.onkeydown = function (e) {
  let data = {
    keyCode: e.keyCode,
    shift: e.shiftKey,
    meta: e.metaKey,
    control: e.ctrlKey,
    alt: e.altKey,
  };
  peer.emit("robot", "key", data);
};

// 监听鼠标事件
window.onmouseup = function (e) {
  let data = {};
  data.clientX = e.clientX;
  data.clientY = e.clientY;
  data.video = {
    width: video.getBoundingClientRect().width,
    height: video.getBoundingClientRect().height,
  };
  peer.emit("robot", "mouse", data);
};
